<#assign className = table.className>
<#assign classNameLower = table.classNameLower><#if  '${subpackage}'=="">
package ${basepackage}.controller;
<#else>
package ${basepackage}.${subpackage}.controller;
</#if>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.yr.dj.util.PageQueryConversion;
import com.yr.dj.util.dt.PageQuery;
import com.yr.dj.util.dt.SearchData;

import io.swagger.annotations.*;
import ${basepackage}.${subpackage}.intf.entity.${className};
import ${basepackage}.${subpackage}.service.mapper.${className}Mapper;

import java.util.Date;

<#include "java_copyright.include">

@RestController
@RequestMapping("/${table.sqlName}")
@Api(value = "${className}Controller", description = "${table.tableRemark}")
public class ${className}Controller  {

    @Autowired
	private ${className}Mapper ${classNameLower}Mapper;


    @PostMapping("/save_or_update")
    @ApiOperation("新增或者更新")
    @Transactional
    public boolean saveOrUpdate(@RequestBody ${className} ${classNameLower},@RequestParam("userId") Integer userId){

        if(${classNameLower}.getId()==null){<#if table.hasCreator >
             ${classNameLower}.setCreator(userId);</#if><#if table.hasCreateTime >
             ${classNameLower}.setCreateTime(new Date());</#if>
             return ${classNameLower}Mapper.insertUseGeneratedKeys(${classNameLower})>0;
        }
        ${className} dbData = ${classNameLower}Mapper.selectByPrimaryKey(${classNameLower}.getId());

        if(dbData==null)
            return false;<#if table.hasEditor >
        ${classNameLower}.setEditor(userId);</#if><#if table.hasEditTime >
        ${classNameLower}.setEditTime(new Date());</#if><#if table.hasCreator >
        ${classNameLower}.setCreator(dbData.getCreator());</#if><#if table.hasCreateTime>
        ${classNameLower}.setCreateTime(dbData.getCreateTime());</#if>
        return ${classNameLower}Mapper.updateByPrimaryKey(${classNameLower})>0;
	}

    @PostMapping("/update_selective")
    @ApiOperation("根据主键更新属性不为null的值")
    public boolean updateSelective(@RequestBody ${className} ${classNameLower}){
        return ${classNameLower}Mapper.updateByPrimaryKeySelective(${classNameLower})>0;
    }

    @PostMapping("/delete")
    @ApiOperation("删除数据")
    public boolean delete(@RequestParam("id") Integer id){
        return ${classNameLower}Mapper.deleteByPrimaryKey(id)>0;
    }

    @PostMapping("/load_by_search")
    @ApiOperation("查询列表")
    public PageInfo<${className}> loadBySearch(@RequestBody PageQuery query){

        SearchData searchData=query.getSearchData();
        boolean result=PageQueryConversion.isParamitersPass(searchData.getParamiters());
        if(result){
            return null;
        }
        searchData.setQuery(PageQueryConversion.transformPageQuery2String(searchData));
        PageInfo<${className}> pageInfo=PageHelper.startPage(query.getPageNum(),query.getPageSize())
                                         .doSelectPageInfo(()->${classNameLower}Mapper.loadBySearch(query.getSearchData()));
        return pageInfo;
    }

    @ApiOperation("提供给API端的分页查询")
    @PostMapping("/load_by_entity")
    public PageInfo<${className}>loadByEntity(@RequestParam("pageNum") @ApiParam("当前页")Integer pageNum,
                                         @RequestParam("pageSize") @ApiParam("每页多少行") Integer pageSize,
                                         @RequestBody ${className} ${classNameLower}){

        return PageHelper.startPage(pageNum,pageSize)
                .doSelectPageInfo(()->${classNameLower}Mapper.select(${classNameLower}));
    }

    @ApiOperation("查询单行数据")
    @PostMapping("/load_one")
    public ${className} loadByCode(@RequestBody ${className} entity){

        return ${classNameLower}Mapper.selectOne(entity);
    }
}

